/* ****版权所有
 */
package com.wxy.util;

import java.sql.*;

/**
 * 数据库实用类：连接的是MySQL8
 * @author wxy
 * @version 1.0
 */
public class DBUtil {
    /**
     * 获得数据库连接
     * @return 数据库对象
     */
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=utf-8";
            conn = DriverManager.getConnection(url,"root","1234");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭数据库资源
     * @param rs  结果集
     * @param pstmt  预编译对象
     * @param conn  数据库连接
     */
    public static void close(ResultSet rs, PreparedStatement pstmt,Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (pstmt !=null){
            try {
                pstmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
